//
//  WSJSliderView.m
//  RunningMan
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSJSliderView.h"
#define SliderWidth 240
#define SliderHeight 40
#define SliderLabelTextColor [UIColor colorWithRed:193/255.0 green:193/255.0 blue:193/255.0 alpha:1]
#define SliderLabelBorderColor [UIColor colorWithRed:193/255.0 green:193/255.0 blue:193/255.0 alpha:1].CGColor
#define SliderMinimumTrackTintColor [UIColor redColor]
#define SliderLabelFont 15
#define SliderLabelText @"滑动解锁"
#define ThumbImageWidth 40
#define ThumbImageHeight 40


@interface WSJSliderView (){
    UIImageView *imgView;
}

@property (nonatomic ,strong)UILabel *label;
@property(nonatomic, retain) UILabel *redLabel;
@end

@implementation WSJSliderView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSlider];
    }
    return self;
}

- (void)createSlider{
    self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, SliderWidth, SliderHeight)];
  
    self.slider.minimumTrackTintColor = [UIColor clearColor];
    self.slider.maximumTrackTintColor = [UIColor clearColor];
    self.slider.layer.masksToBounds = YES;
    self.slider.layer.cornerRadius = SliderHeight/2;
    [self.slider setThumbImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.slider];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SliderWidth, SliderHeight)];
     self.label .text = SliderLabelText;
     self.label .font = [UIFont systemFontOfSize:SliderLabelFont];
     self.label .textAlignment = NSTextAlignmentCenter;
     self.label .textColor = SliderLabelTextColor;
     self.label .layer.masksToBounds = YES;
     self.label .layer.cornerRadius = SliderHeight/2;
     self.label .layer.borderWidth = 1;
     self.label .layer.borderColor = SliderLabelBorderColor;
    [self addSubview:self.label];
    
    self.redLabel = [[UILabel alloc]init];
    self.redLabel .layer.masksToBounds = YES;
    self.redLabel .layer.cornerRadius = SliderHeight/2;
    self.redLabel .layer.borderWidth = 1;
    self.redLabel .layer.borderColor = SliderLabelBorderColor;
    self.redLabel.backgroundColor = [UIColor orangeColor];
    [self addSubview:self.redLabel];
    
    
    
    //这里创建了一个跟滑块相同的imageview覆盖在文字上面，并在sliderValueChanged方法中让其跟着滑块滑动。
    imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.slider.frame.origin.x-2, self.slider.frame.origin.y-2, ThumbImageWidth+4, ThumbImageHeight+4)];
    imgView.image = [UIImage imageNamed:@"2.png"];
    [self addSubview:imgView];
}
- (void)sliderValueChanged:(UISlider *)slider{
    [self.slider setValue:slider.value animated:NO];
    if (slider.value >0) {
        self.slider.minimumTrackTintColor = SliderMinimumTrackTintColor;
    }else{
        self.slider.minimumTrackTintColor = [UIColor clearColor];
    }
    imgView.center = CGPointMake(self.slider.frame.origin.x+slider.value*(SliderWidth-ThumbImageWidth)+ThumbImageWidth/2, self.slider.frame.origin.y+ThumbImageHeight/2);
    if (!slider.isTracking && slider.value != 1) {
        [self.slider setValue:0 animated:YES];
        if (slider.value >0) {
            self.slider.minimumTrackTintColor = SliderMinimumTrackTintColor;
        }else{
            self.slider.minimumTrackTintColor = [UIColor clearColor];
        }
        imgView.frame = CGRectMake(self.slider.frame.origin.x-2, self.slider.frame.origin.y-2, ThumbImageWidth+4, ThumbImageHeight+4);
    }
    
    if (slider.value < 0.5) {
        self.redLabel.frame = CGRectMake(0, 0, SliderWidth * slider.value + SliderHeight / 2, SliderHeight);
    } else {
        self.redLabel.frame = CGRectMake(0, 0, SliderWidth * slider.value, SliderHeight);
    }
  
    if (slider.value == 1.0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:nil];
         [self.slider setValue:0 animated:YES];
          imgView.center = CGPointMake(self.slider.frame.origin.x+self.slider.value*(SliderWidth-ThumbImageWidth)+ThumbImageWidth/2, self.slider.frame.origin.y+ThumbImageHeight/2);
        self.redLabel.frame = CGRectMake(0, 0, 0, 0);
    }
    
}

@end
